/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-10-03
 * Time: 11:36
 */
public class BinarySearchTree {
    //创建一个节点
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    //把根节点变成一个全局变量
    public TreeNode root;

    //插入数据
    public void insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        TreeNode node = new TreeNode(key);
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                //一颗搜索二叉树不能出现2个相同的节点
                return;
            }
        }
        //走到要插入的位置
        if (parent.val > key) {
            parent.left = node;
        } else {
            parent.right = node;
        }
    }

    /**
     * 时间复杂度：
     * 最好情况：O(logN)（是一个红黑树【满二叉树】）
     * 最坏情况：O(N)（是一棵单分枝的树）
     *
     * @param key
     * @return
     */
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val > key) {
                cur = cur.left;
            } else if (cur.val == key) {
                return cur;
            } else {
                cur = cur.right;
            }
        }
        return null;
    }

    //二叉搜索树的删除；比较复杂
    public void remove(int key) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (cur.val > key) {
                parent = cur;
                cur = cur.left;
            } else if (cur.val < key) {
                parent = cur;
                cur = cur.right;
            } else {
                removeNode(parent, cur);
                return;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            } else {
                parent.right = cur.right;
            }
        } else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            } else if (cur == parent.left) {
                parent.left = cur.left;
            } else {
                parent.right = cur.left;
            }
        } else {// 往右边找最小值：在右子树的最左边
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if (targetParent.left == target) {
                targetParent.left = target.right;
            } else {
                targetParent.right = target.right;
            }
        }
    }
}
